#include "markedscrollbar.h"

#include <QtGui>

MarkedScrollBar::MarkedScrollBar(QWidget *parent) :
	QScrollBar(parent)
{
}

MarkedScrollBar::MarkedScrollBar(Qt::Orientation o, QWidget *parent) : QScrollBar(o, parent)
{
}

void MarkedScrollBar::setBg(QImage bg)
{
	this->bg = bg;
	update();
}

QRect MarkedScrollBar::drawableRect()
{
	QStyleOptionSlider opt;
	initStyleOption(&opt);
	opt.subControls = QStyle::SC_All;

	QRect scrollBarSubPage = style()->subControlRect(QStyle::CC_ScrollBar, &opt, QStyle::SC_ScrollBarAddPage, this).adjusted(1, 0, -1, 0);
	QRect scrollBarAddPage = style()->subControlRect(QStyle::CC_ScrollBar, &opt, QStyle::SC_ScrollBarSubPage, this).adjusted(1, 0, -1, 0);
	return QRect(scrollBarAddPage.topLeft(), scrollBarSubPage.bottomRight());
}

void MarkedScrollBar::paintEvent(QPaintEvent *e)
{
	QScrollBar::paintEvent(e);

	QStyleOptionSlider opt;
	initStyleOption(&opt);
	opt.subControls = QStyle::SC_All;

	QRect scrollBarSubPage = style()->subControlRect(QStyle::CC_ScrollBar, &opt, QStyle::SC_ScrollBarAddPage, this).adjusted(1, 0, -1, 0);
	QRect scrollBarAddPage = style()->subControlRect(QStyle::CC_ScrollBar, &opt, QStyle::SC_ScrollBarSubPage, this).adjusted(1, 0, -1, 0);

	QPainter painter(this);
	painter.setClipRect(scrollBarAddPage);
	painter.drawImage(scrollBarAddPage.topLeft(), bg);
	painter.setClipRect(scrollBarSubPage);
	painter.drawImage(scrollBarAddPage.topLeft(), bg);
}
